Skip to content

Add restart command and --watch option to wp shell#77

Open
Copilot wants to merge 13 commits intomainfrom
copilot/support-restarting-shell
Open

Add restart command and --watch option to wp shell#77
Copilot wants to merge 13 commits intomainfrom
copilot/support-restarting-shell

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

  • Understand current codebase structure and how REPL works
  • Implement restart functionality in REPL class
    • Add special command detection for 'restart'
    • Return special exit code (10) when restart is requested
    • Update REPL loop to return instead of infinite loop on restart
    • Add 'exit' command support for clean exit
  • Implement wrapper loop in Shell_Command to restart REPL
    • Check return value and restart if needed
    • Handle restart loop with do-while pattern
    • Spawn new PHP process using pcntl_exec() to fully reload all code
    • Preserve command arguments (--basic, --watch, --path) when restarting
    • Fallback to in-process restart if pcntl_exec() unavailable
  • Add comprehensive tests for restart and exit functionality
  • Implement --watch option for auto-restart on file changes
    • Add --watch parameter handling with validation
    • Add file modification time tracking
    • Implement recursive directory monitoring
    • Auto-restart when changes detected
    • Force --basic mode when --watch is used
  • Update documentation in docblocks with examples
  • Document pcntl_exec() requirement and fallback behavior
  • Run code review (passed with no issues)
  • Run security checks (no issues found)
  • Fix PHPCS alignment issues
  • Remove unnecessary PHPStan ignore directive
  • Add SplFileInfo type hint for PHPStan

Implementation Complete

The restart command now spawns a new PHP process using pcntl_exec(), which fully reloads all code including files loaded with require_once/include_once. This addresses the original issue requirement to "properly spawn the actual REPL in a new PHP process" for reloading modified code. If pcntl_exec() is not available, the shell falls back to in-process restart with a debug message.

All requirements from the issue have been implemented and code style issues have been addressed.

Original prompt

This section details on the original issue you should resolve

<issue_title>Support restarting a shell</issue_title>
<issue_description>The wp shell command is super useful for debugging sessions, however it is quite tedious when you need to make code changes, as it does not reload the modified code.

By properly spawning the actual REPL in a new PHP process, and by checking the exit state for a specific result, it should be possible to inject a command that allows for restarting the REPL.

Bonus points: provide a wp shell --watch=<folder> command that watches for changes in a specific folder and restarts the shell automatically.</issue_description>

Comments on the Issue (you are @copilot in this section)

@danielbachhuber 💯

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 30, 2025 12:22
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Support restarting the shell in wp shell command Add restart command and --watch option to wp shell Oct 30, 2025
Copilot AI requested a review from swissspidy October 30, 2025 12:31
@codecov
Copy link

codecov bot commented Oct 30, 2025

Codecov Report

❌ Patch coverage is 14.60674% with 76 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Shell_Command.php 16.32% 41 Missing ⚠️
src/WP_CLI/Shell/REPL.php 12.50% 35 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy swissspidy marked this pull request as ready for review October 30, 2025 21:25
@swissspidy swissspidy requested a review from a team as a code owner October 30, 2025 21:25

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy

This comment was marked as resolved.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This PR introduces very useful restart and --watch features to wp shell. The implementation is well-structured, especially the use of a special exit code for restarting and the pcntl_exec for a full process reload. The file watching logic is also robustly implemented with recursive directory scanning and proper error handling.

I've added a couple of suggestions to improve the robustness of argument handling during the restart process. Also, while there are good tests for the restart and exit commands, consider adding tests for the new --watch functionality to prevent future regressions.

Comment on lines +175 to +182
// Add the path argument if present in $_SERVER
if ( isset( $_SERVER['argv'] ) && is_array( $_SERVER['argv'] ) ) {
foreach ( $_SERVER['argv'] as $arg ) {
if ( is_string( $arg ) && '--path=' === substr( $arg, 0, 7 ) ) {
$args[] = $arg;
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation for preserving the --path argument is fragile. It only works for the --path=/some/path format and will fail if the argument is passed with a space, like --path /my/path. This can lead to the shell restarting in an incorrect directory.

A more robust approach is to retrieve the configuration value directly from WP-CLI's runner. This would also make it easier to preserve other important global parameters (e.g., --user, --url) to ensure a consistent environment after restarting.

		// Add the path argument if present from the runner's config.
		$config = WP_CLI::get_runner()->config;
		if ( isset( $config['path'] ) ) {
			$args[] = '--path=' . $config['path'];
		}

Comment on lines +147 to +156
$wp_cli_script = null;
foreach ( array( 'argv', '_SERVER' ) as $source ) {
if ( 'argv' === $source && is_array( $GLOBALS['argv'] ) && isset( $GLOBALS['argv'][0] ) ) {
$wp_cli_script = $GLOBALS['argv'][0];
break;
} elseif ( '_SERVER' === $source && is_array( $_SERVER['argv'] ) && isset( $_SERVER['argv'][0] ) ) {
$wp_cli_script = $_SERVER['argv'][0];
break;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to determine the WP-CLI script path is unnecessarily complex. It can be simplified for better readability and maintainability by using a direct if/elseif check on $GLOBALS['argv'] and $_SERVER['argv'] instead of a foreach loop.

		$wp_cli_script = null;
		if ( isset( $GLOBALS['argv'][0] ) ) {
			$wp_cli_script = $GLOBALS['argv'][0];
		} elseif ( isset( $_SERVER['argv'][0] ) ) {
			$wp_cli_script = $_SERVER['argv'][0];
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support restarting a shell

2 participants